package com.cl.edu.service.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cl.core.orm.Page;
import com.cl.core.orm.SearchFilter;
import com.cl.core.orm.hibernate.HibernateWebUtils;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.dao.BookingstudentDAO;
import com.cl.edu.dao.BookingteacherDAO;
import com.cl.edu.dao.StudentDAO;
import com.cl.edu.entity.Student;

/**
 * 学员信息管 理
 * @author Administrator
 *
 */
@Service
@Transactional
public class StudentsManager {
	@Autowired
	private StudentDAO studentDAO;
	@Autowired
	private BookingstudentDAO bookingstudentDAO;
	@Autowired
	private BookingteacherDAO bookingteacherDAO;
	
	/**
	 * 根据id得到学员信息
	 * @param id
	 * @return
	 */
	public Student getStudent(Long id) {
		return studentDAO.get(id);
	}
	
	/**
	 * 删除学员
	 * @param id
	 */
	public void delStudent(Long id) {
		studentDAO.delete(id);
	}
	
	/**
	 * 更新学员
	 * @param entity
	 */
	public void updateStudent(Student entity) {
		studentDAO.saveOrUpdate(entity);
	}
	
	/**
	 * 分页查询学员信息
	 * @param page
	 * @return
	 */
	public Page<Student> getStudents(Page<Student> page,int state) {
		StringBuffer hql = new StringBuffer("from Student model ");
		hql.append(" where 1=1 and model.state = ").append(state);
		List<SearchFilter> filters = HibernateWebUtils.buildSearchFilters(
				Struts2Utils.getRequest(), "filter_");
		for (SearchFilter filter : filters) {
			if ("id".equals(filter.getPropertyName())) {
				hql.append(" and model.id = ")
						.append(filter.getPropertyValue());
			} else if ("name".equals(filter.getPropertyName())) {
				hql.append(" and model.name like '%")
						.append(filter.getPropertyValue()).append("%'");
			}
		}
		hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
		page = studentDAO.findPage(page, hql.toString());
		return page;
	}
	/**
	 * 删除教员主动预约学生表中指定教师编号的预约记录
	 * @param studentId
	 */
	public void delBookingstudents(Long studentId){
		bookingstudentDAO.batchExecute("delete from Bookingstudent model where model.student.id="+studentId);
	}
	
	/**
	 * 删除学生主动预约教员表中指定教师编号的预约记录
	 * @param studentId
	 */
	public void delBookingteachers(Long studentId) {
		bookingteacherDAO.batchExecute("delete from Bookingteacher model where model.student.id="+studentId);
	}
}
